Passed
Push — renovate/gatsby-parallel-runne... ( 92a788 )
by
unknown
04:57
created

seo.js ➔ SEO   A

Complexity

Conditions 3

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 50
rs 9.0399
c 0
b 0
f 0
cc 3
1
import React from "react"
2
import PropTypes from "prop-types"
3
import { Helmet } from "react-helmet"
4
import { StaticQuery, graphql } from "gatsby"
5
6
import defaultOgImage from "../images/preseason.jpg"
7
8
// function SEO({ description, lang, meta, keywords, title }) {
9
function SEO({
10
  lang,
11
  title,
12
  description,
13
  meta,
14
  keywords,
15
  path,
16
  image: metaImage,
17
}) {
18
  return (
19
    <StaticQuery
20
      query={detailsQuery}
21
      render={({ site }) => {
22
        const metaDescription = description || site.siteMetadata.description
23
        const canonicalUrl = path ? `${site.siteMetadata.siteUrl}${path}` : null
24
25
        return (
26
          <Helmet
27
            htmlAttributes={{
28
              lang,
29
            }}
30
            title={title}
31
            titleTemplate={`%s | ${site.siteMetadata.title}`}
32
            link={
33
              canonicalUrl
34
                ? [
35
                    {
36
                      rel: "canonical",
37
                      href: canonicalUrl,
38
                    },
39
                  ]
40
                : []
41
            }
42
            meta={[
43
              {
44
                name: `description`,
45
                content: metaDescription,
46
              },
47
              {
48
                property: `fb:app_id`,
49
                content: site.siteMetadata.fbAppId,
50
              },
51
            ]
52
              .concat(getOgMeta(title, metaDescription, canonicalUrl))
53
              .concat(getOgImage(site, metaImage, title))
54
              .concat(getTwitterMeta(site, title, metaDescription, metaImage))
55
              .concat(getKeywords(keywords))
56
              .concat(meta)}
57
          />
58
        )
59
      }}
60
    />
61
  )
62
}
63
const getOgMeta = (title, metaDescription, canonicalUrl) => {
64
  const ogMeta = [
65
    {
66
      property: `og:title`,
67
      content: title,
68
    },
69
    {
70
      property: `og:description`,
71
      content: metaDescription,
72
    },
73
    {
74
      property: `og:type`,
75
      content: `website`,
76
    },
77
  ].concat(getOgUrl(canonicalUrl))
78
  return ogMeta
79
}
80
81
const getTwitterMeta = (site, title, metaDescription, metaImage) => {
82
  const twitterMeta = [
83
    {
84
      name: `twitter:creator`,
85
      content: site.siteMetadata.author,
86
    },
87
    {
88
      name: `twitter:title`,
89
      content: title,
90
    },
91
    {
92
      name: `twitter:description`,
93
      content: metaDescription,
94
    },
95
  ].concat(getTwitterCard(site, metaImage))
96
  return twitterMeta
97
}
98
99
const getOgImage = ({ siteMetadata }, metaImage, title) => {
100
  const image =
101
    metaImage && metaImage.src
102
      ? `${siteMetadata.siteUrl}${metaImage.src}`
103
      : null
104
  return metaImage
105
    ? [
106
        {
107
          property: `og:image`,
108
          content: image,
109
        },
110
        {
111
          property: `og:image:width`,
112
          content: metaImage.width,
113
        },
114
        {
115
          property: `og:image:type`,
116
          content: "image/jpeg",
117
        },
118
        {
119
          property: `og:image:height`,
120
          content: metaImage.height,
121
        },
122
        {
123
          property: `og:image:title`,
124
          content: title,
125
        },
126
        {
127
          property: `og:image:alt`,
128
          content: title,
129
        },
130
      ]
131
    : []
132
}
133
134
const getTwitterCard = (metaImage) =>
135
  metaImage
136
    ? {
137
        name: "twitter:card",
138
        content: "summary_large_image",
139
      }
140
    : {
141
        name: "twitter:card",
142
        content: "summary",
143
      }
144
145
const getOgUrl = (canonicalUrl) =>
146
  canonicalUrl
147
    ? {
148
        property: `og:url`,
149
        content: canonicalUrl,
150
      }
151
    : []
152
153
const getKeywords = (keywords) =>
154
  keywords.length > 0
155
    ? {
156
        name: `keywords`,
157
        content: keywords.join(`, `),
158
      }
159
    : []
160
161
SEO.defaultProps = {
162
  lang: `nl-BE`,
163
  meta: [],
164
  keywords: [],
165
  path: "/",
166
  image: {
167
    src: defaultOgImage,
168
    width: 2000,
169
    height: 1000,
170
  },
171
}
172
173
SEO.propTypes = {
174
  subtitle: PropTypes.string,
175
  description: PropTypes.string,
176
  lang: PropTypes.string,
177
  meta: PropTypes.array,
178
  keywords: PropTypes.arrayOf(PropTypes.string),
179
  title: PropTypes.string.isRequired,
180
  path: PropTypes.string,
181
  image: PropTypes.shape({
182
    src: PropTypes.string.isRequired,
183
    height: PropTypes.number.isRequired,
184
    width: PropTypes.number.isRequired,
185
  }),
186
}
187
188
export default SEO
189
190
const detailsQuery = graphql`
191
  query DefaultSEOQuery {
192
    site {
193
      siteMetadata {
194
        title
195
        subTitle
196
        description
197
        author
198
        siteUrl
199
        fbAppId
200
      }
201
    }
202
  }
203
`
204